package com.example.service.impl;

import com.example.pojo.DemoData;
import com.example.pojo.Journal;
import com.example.pojo.JournalField;
import com.example.pojo.Poi;
import com.example.service.ExcelService;
import com.example.utils.ExcelUtils;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class ExcelServiceImpl implements ExcelService {
    @Override
    public String operate(String path) {
        List<DemoData> list = ExcelUtils.readExcel(path, DemoData.class);
        if(list==null){
            return "文件不存在";
        }
        if(list.isEmpty()){
            return "头部字段错误";
        }
        ExcelUtils.writeExcel(path,list,DemoData.class);
        return "成功";
    }

    @Override
    public String handleExcel(String path) {
        //获取工作薄
        Workbook workbook = ExcelUtils.readWorkbook(path);
        if(workbook==null){
            return "文件不存在或出异常";
        }
        //处理数据
        Sheet sheet = workbook.getSheetAt(0);
        for (int j=1;j<=sheet.getLastRowNum();j++){
            Row row=sheet.getRow(j);
            double max=0;
            for (int i=3;i<row.getLastCellNum();i++){
                Cell cell=row.getCell(i);
                if(cell==null){
                    continue;
                }
                double numericCellValue = cell.getNumericCellValue();
                if(numericCellValue>max){
                    max=numericCellValue;
                }
            }
            row.getCell(2).setCellValue(max);
        }
        //写出数据
        ExcelUtils.writeWorkbook(path,workbook);
        return "成功";
    }

    @Override
    public String handleMalposition(String path) {
        List<Journal> list=new ArrayList<>();
        //获取工作薄
        Workbook workbook = ExcelUtils.readWorkbook(path);
        if(workbook==null){
            return "文件不存在或出异常";
        }
        Sheet sheet = workbook.getSheetAt(0);
        for(int i=1;i<=sheet.getLastRowNum();i++){
            Row row=sheet.getRow(i);
            Journal journal=new Journal();
            for (int j=0;j<row.getLastCellNum();j++) {
                Cell cell = row.getCell(j);
                if(cell==null){
                    continue;
                }
                if(j==0){
                    journal.setName(cell.getStringCellValue());
                    continue;
                }
                if(j==1){
                    journal.setNameEnglish(cell.getStringCellValue());
                    continue;
                }
                if(j==2){
                    journal.setCover(cell.getStringCellValue());
                    continue;
                }
                if(j+1==row.getLastCellNum()){
                    journal.setOther(cell.getStringCellValue());
                    continue;
                }
                if(cell.getStringCellValue().startsWith("曾用刊名")){
                    journal.setNameOnce(cell.getStringCellValue());
                } else if (cell.getStringCellValue().startsWith("期刊级别")) {
                    journal.setLevel(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("出版周期")) {
                    journal.setPublishingCycel(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("主管单位")) {
                    journal.setSponsor(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("期刊分类")) {
                    journal.setCategory(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("主办单位")) {
                    journal.setOrganizer(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("邮发代号")) {
                    journal.setPostalCode(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("创刊时间")) {
                    journal.setCreateTime(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("issn")) {
                    journal.setIssn(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("cn")) {
                    journal.setCn(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("期刊语言")) {
                    journal.setLanguage(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("主编")) {
                    journal.setEditorChief(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("邮编")) {
                    journal.setPostCode(cell.getStringCellValue());
                } else if (cell.getStringCellValue().startsWith("单价")) {
                    journal.setPrice(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("目前状态")) {
                    journal.setStatus(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("出版地")) {
                    journal.setPlace(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("语种")) {
                    journal.setLanguageType(cell.getStringCellValue());
                }else if (cell.getStringCellValue().startsWith("开本")) {
                    journal.setBookSize(cell.getStringCellValue());
                }
            }
            list.add(journal);
        }
        //导出数据
        ExcelUtils.writeExcel(path,list, Journal.class);
        return "成功";
    }

    @Override
    public String replenishIssn(String originalPath, String issnPath) {
        List<Journal> journalList = ExcelUtils.readExcel(originalPath, Journal.class);
        List<JournalField> journalFieldList = ExcelUtils.readExcel(issnPath, JournalField.class);
        if(journalList==null||journalFieldList==null){
            return "文件不存在";
        }
        //补充issn
        int count=0;
        for (Journal journal : journalList) {
            if(journal.getIssn()!=null||journal.getName()==null){
                continue;
            }
            List<JournalField> journalFields=new ArrayList<>();
            for (JournalField journalField : journalFieldList) {
                if(journalField.getName()==null||journalField.getIssn()==null){
                    continue;
                }
                if(extractChinese(journal.getName()).equals(extractChinese(journalField.getName()))){
                    journalFields.add(journalField);
                }
            }
            if(journalFields.size()<=1){
                if(journalFields.size()==1){
                    journal.setIssn(journalFields.get(0).getIssn());
                    count++;
                }
                continue;
            }
            //寻找合适的issn
            for (JournalField journalField : journalFields) {
                if(journal.getOrganizer()!=null&&journalField.getOrganizer()!=null
                        &&journal.getOrganizer().trim().equals(journalField.getOrganizer().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getPublishingCycel()!=null&&journalField.getPublishingCycel()!=null
                        &&journal.getPublishingCycel().trim().equals(journalField.getPublishingCycel().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getPrice()!=null&&journalField.getPrice()!=null
                        &&journal.getPrice().trim().equals(journalField.getPrice().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getCreateTime()!=null&&journalField.getCreateTime()!=null
                        &&journal.getCreateTime().trim().equals(journalField.getCreateTime().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
            }
            JournalField journalField1=journalFields.get(0);
            for (int i=1;i<journalFields.size();i++) {
                if(journalFields.get(i).getScore()>journalField1.getScore()){
                    journalField1=journalFields.get(i);
                }
            }
            journal.setIssn(journalField1.getIssn());
            count++;
            //把分数设置为0，避免影响下一轮
            for (int i=0;i<journalFields.size();i++) {
                journalFields.get(i).setScore(0);
            }
        }
        ExcelUtils.writeExcel(originalPath,journalList,Journal.class);
        return "成功（补充了issn"+count+"个）";
    }

    @Override
    public String addIssn2(String originalPath, String issn2Path) {
        List<Journal> journalList = ExcelUtils.readExcel(originalPath, Journal.class);
        List<JournalField> journalFieldList = ExcelUtils.readExcel(issn2Path, JournalField.class);
        if(journalList==null||journalFieldList==null){
            return "文件不存在";
        }
        //补充issn2
        int count=0;
        for (Journal journal : journalList) {
            if(journal.getIssn2()!=null||journal.getName()==null){
                continue;
            }
            List<JournalField> journalFields=new ArrayList<>();
            for (JournalField journalField : journalFieldList) {
                if(journalField.getName()==null||journalField.getIssn()==null){
                    continue;
                }
                if(journal.getName().equals(journalField.getName())){
                    journalFields.add(journalField);
                }
            }
            if(journalFields.size()<=1){
                if(journalFields.size()==1){
                    journal.setIssn2(journalFields.get(0).getIssn());
                    count++;
                }
                continue;
            }
            //寻找合适的issn2
            for (JournalField journalField : journalFields) {
                if(journal.getCover()!=null&&journalField.getCover()!=null
                        &&journal.getCover().trim().equals(journalField.getCover().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getStatus()!=null&&journalField.getStatus()!=null
                        &&journal.getStatus().trim().equals(journalField.getStatus().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getBrowseNum()!=null&&journalField.getBrowseNum()!=null
                        &&journal.getBrowseNum().trim().equals(journalField.getBrowseNum().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getIssn()!=null&&journalField.getIssn()!=null
                        &&journal.getIssn().trim().equals(journalField.getIssn().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getCn()!=null&&journalField.getCn()!=null
                        &&journal.getCn().trim().equals(journalField.getCn().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getOrganizer()!=null&&journalField.getOrganizer()!=null
                        &&journal.getOrganizer().trim().equals(journalField.getOrganizer().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getPublishingCycel()!=null&&journalField.getPublishingCycel()!=null
                        &&journal.getPublishingCycel().trim().equals(journalField.getPublishingCycel().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getPrice()!=null&&journalField.getPrice()!=null
                        &&journal.getPrice().trim().equals(journalField.getPrice().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getCreateTime()!=null&&journalField.getCreateTime()!=null
                        &&journal.getCreateTime().trim().equals(journalField.getCreateTime().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
            }
            JournalField journalField1=journalFields.get(0);
            for (int i=1;i<journalFields.size();i++) {
                if(journalFields.get(i).getScore()>journalField1.getScore()){
                    journalField1=journalFields.get(i);
                }
            }
            journal.setIssn2(journalField1.getIssn());
            count++;
            //把分数设置为0，避免影响下一轮
            for (int i=0;i<journalFields.size();i++) {
                journalFields.get(i).setScore(0);
            }
        }
        ExcelUtils.writeExcel(originalPath,journalList,Journal.class);
        return "成功（增加了issn2("+count+")个）";
    }

    @Override
    public String reserveIssn(String originalPath, String issnPath, String issnPath2, String issnPath3) {
        //获取excel中的数据
        List<Journal> journalList = ExcelUtils.readExcel(originalPath, Journal.class);
        List<JournalField> journalFieldList = ExcelUtils.readExcel(issnPath, JournalField.class);
        List<JournalField> journalFieldList2 = ExcelUtils.readExcel(issnPath2, JournalField.class);
        List<JournalField> journalFieldList3 = ExcelUtils.readExcel(issnPath3, JournalField.class);
        if(journalList==null||journalFieldList==null||journalFieldList2==null||journalFieldList3==null){
            return "文件不存在";
        }
        for (Journal journal : journalList) {
            if(journal.getName()==null||journal.getName2()==null||journal.getIssn2()!=null){
                continue;
            }
            boolean isFind=false;
            for (JournalField field : journalFieldList) {
                if(field.getName()==null||field.getIssn()==null){
                    continue;
                }
                if(extractChinese(journal.getName()).equals(extractChinese(field.getName()))
                        ||extractChinese(journal.getName2()).equals(field.getName())){
                    journal.setIssnDelFlag(0);
                    isFind=true;
                    break;
                }
            }
            if(isFind){
               continue;
            }
            for (JournalField field2 : journalFieldList2) {
                if(field2.getName()==null||field2.getIssn()==null){
                    continue;
                }
                if(extractChinese(journal.getName()).equals(extractChinese(field2.getName()))
                        ||extractChinese(journal.getName2()).equals(field2.getName())){
                    journal.setIssnDelFlag(0);
                    isFind=true;
                    break;
                }
            }
            if(isFind){
                continue;
            }
            for (JournalField field3 : journalFieldList3) {
                if(field3.getName()==null||field3.getIssn()==null){
                    continue;
                }
                if(extractChinese(journal.getName()).equals(extractChinese(field3.getName()))
                        ||extractChinese(journal.getName2()).equals(field3.getName())){
                    journal.setIssnDelFlag(0);
                    isFind=true;
                    break;
                }
            }
        }
        ExcelUtils.writeExcel(originalPath,journalList,Journal.class);
        return "成功";
    }

    @Override
    public String addPriceField(String originalPath, String fieldPath) {
        //获取excel中的数据
        List<Journal> journalList = ExcelUtils.readExcel(originalPath, Journal.class);
        List<JournalField> journalFieldList = ExcelUtils.readExcel(fieldPath, JournalField.class);
        if(journalList==null||journalFieldList==null){
            return "文件不存在";
        }
        //增加价格数据
        int count=0;
        for (Journal journal : journalList) {
            if(journal.getName()==null||journal.getPrice()!=null){
                continue;
            }
            List<JournalField> journalFields=new ArrayList<>();
            for (JournalField journalField : journalFieldList) {
                if(journalField.getName()==null||journalField.getPrice()==null){
                    continue;
                }
                if(extractChinese(journal.getName()).equals(extractChinese(journalField.getName()))){
                    journalFields.add(journalField);
                }
            }
            if(journalFields.size()<=1){
                if(journalFields.size()==1){
                    journal.setPrice(journalFields.get(0).getPrice());
                    count++;
                }
                continue;
            }
            //寻找合适的价格
            for (JournalField journalField : journalFields) {
                if(journal.getOrganizer()!=null&&journalField.getOrganizer()!=null
                        &&journal.getOrganizer().trim().equals(journalField.getOrganizer().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getPublishingCycel()!=null&&journalField.getPublishingCycel()!=null
                        &&journal.getPublishingCycel().trim().equals(journalField.getPublishingCycel().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getIssn()!=null&&journalField.getIssn()!=null
                        &&journal.getIssn().trim().equals(journalField.getIssn().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getCreateTime()!=null&&journalField.getCreateTime()!=null
                        &&journal.getCreateTime().trim().equals(journalField.getCreateTime().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
            }
            JournalField journalField1=journalFields.get(0);
            for (int i=1;i<journalFields.size();i++) {
                if(journalFields.get(i).getScore()>journalField1.getScore()){
                    journalField1=journalFields.get(i);
                }
            }
            journal.setPrice(journalField1.getPrice());
            count++;
            //把分数设置为0，避免影响下一轮
            for (int i=0;i<journalFields.size();i++) {
                journalFields.get(i).setScore(0);
            }
        }
        ExcelUtils.writeExcel(originalPath,journalList,Journal.class);
        return "成功（增加了价格"+count+"个）";
    }

    @Override
    public String addCreateTimeField(String originalPath, String fieldPath) {
        List<Journal> journalList = ExcelUtils.readExcel(originalPath, Journal.class);
        List<JournalField> journalFieldList = ExcelUtils.readExcel(fieldPath, JournalField.class);
        if(journalList==null||journalFieldList==null){
            return "文件不存在";
        }
        //增加创刊时间数据
        int count=0;
        for (Journal journal : journalList) {
            if(journal.getCreateTime()!=null||journal.getName()==null){
                continue;
            }
            List<JournalField> journalFields=new ArrayList<>();
            for (JournalField journalField : journalFieldList) {
                if(journalField.getName()==null||journalField.getCreateTime()==null){
                    continue;
                }
                if(extractChinese(journal.getName()).equals(extractChinese(journalField.getName()))){
                    journalFields.add(journalField);
                }
            }
            if(journalFields.size()<=1){
                if(journalFields.size()==1){
                    journal.setCreateTime(journalFields.get(0).getCreateTime());
                    count++;
                }
                continue;
            }
            //寻找合适的创刊时间
            for (JournalField journalField : journalFields) {
                if(journal.getOrganizer()!=null&&journalField.getOrganizer()!=null
                        &&journal.getOrganizer().trim().equals(journalField.getOrganizer().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getPublishingCycel()!=null&&journalField.getPublishingCycel()!=null
                        &&journal.getPublishingCycel().trim().equals(journalField.getPublishingCycel().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getPrice()!=null&&journalField.getPrice()!=null
                        &&journal.getPrice().trim().equals(journalField.getPrice().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getIssn()!=null&&journalField.getIssn()!=null
                        &&journal.getIssn().trim().equals(journalField.getIssn().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
            }
            JournalField journalField1=journalFields.get(0);
            for (int i=1;i<journalFields.size();i++) {
                if(journalFields.get(i).getScore()>journalField1.getScore()){
                    journalField1=journalFields.get(i);
                }
            }
            journal.setCreateTime(journalField1.getCreateTime());
            count++;
            //把分数设置为0，避免影响下一轮
            for (int i=0;i<journalFields.size();i++) {
                journalFields.get(i).setScore(0);
            }
        }
        ExcelUtils.writeExcel(originalPath,journalList,Journal.class);
        return "成功（增加了创刊时间"+count+"个）";
    }

    @Override
    public String addOrganizerField(String originalPath,String fieldPath) {
        List<Journal> journalList = ExcelUtils.readExcel(originalPath, Journal.class);
        List<JournalField> journalFieldList = ExcelUtils.readExcel(fieldPath, JournalField.class);
        if(journalList==null||journalFieldList==null){
            return "文件不存在";
        }
        //增加主办单位数据
        int count=0;
        for (Journal journal : journalList) {
            if(journal.getOrganizer()!=null||journal.getName()==null){
                continue;
            }
            List<JournalField> journalFields=new ArrayList<>();
            for (JournalField journalField : journalFieldList) {
                if(journalField.getName()==null||journalField.getOrganizer()==null){
                    continue;
                }
                if(extractChinese(journal.getName()).equals(extractChinese(journalField.getName()))){
                    journalFields.add(journalField);
                }
            }
            if(journalFields.size()<=1){
                if(journalFields.size()==1){
                    journal.setOrganizer(journalFields.get(0).getOrganizer());
                    count++;
                }
                continue;
            }
            //寻找合适的主办单位
            for (JournalField journalField : journalFields) {
                if(journal.getIssn()!=null&&journalField.getIssn()!=null
                        &&journal.getIssn().trim().equals(journalField.getIssn().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getPublishingCycel()!=null&&journalField.getPublishingCycel()!=null
                        &&journal.getPublishingCycel().trim().equals(journalField.getPublishingCycel().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getPrice()!=null&&journalField.getPrice()!=null
                        &&journal.getPrice().trim().equals(journalField.getPrice().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
                if(journal.getCreateTime()!=null&&journalField.getCreateTime()!=null
                        &&journal.getCreateTime().trim().equals(journalField.getCreateTime().trim())){
                    journalField.setScore(journalField.getScore()+1);
                }
            }
            JournalField journalField1=journalFields.get(0);
            for (int i=1;i<journalFields.size();i++) {
                if(journalFields.get(i).getScore()>journalField1.getScore()){
                    journalField1=journalFields.get(i);
                }
            }
            journal.setOrganizer(journalField1.getOrganizer());
            count++;
            //把分数设置为0，避免影响下一轮
            for (int i=0;i<journalFields.size();i++) {
                journalFields.get(i).setScore(0);
            }
        }
        ExcelUtils.writeExcel(originalPath,journalList,Journal.class);
        return "成功（增加了主办单位"+count+"个）";
    }

    @Override
    public String merge(String originalPath, String targetPath) {
        List<Journal> journalList = ExcelUtils.readExcel(originalPath, Journal.class);
        List<Journal> targetList = ExcelUtils.readExcel(targetPath, Journal.class);
        if(journalList==null||targetList==null){
            return "文件不存在";
        }
        List<Journal> mergeList = new ArrayList<>();
        boolean b = mergeList.addAll(journalList);
        if(!b){
            return "集合数据添加失败";
        }
        int count=0;
        for (Journal target : targetList) {
            if(target.getName()==null){
                continue;
            }
            int num=0;
            for (Journal journal : journalList) {
                if(journal.getName()==null){
                    num++;
                    continue;
                }
                if(extractChinese(target.getName()).equals(extractChinese(journal.getName()))){
                    break;
                }
                num++;
            }
            if(journalList.size()==num){
                mergeList.add(target);
                count++;
            }
        }
        ExcelUtils.writeExcel(originalPath,mergeList, Journal.class);
        return "成功（合并了"+count+"个）";
    }

    @Override
    public String deduplicate(String originalPath) {
        List<JournalField> journalFieldList = ExcelUtils.readExcel(originalPath, JournalField.class);
        if(journalFieldList==null){
            return "文件不存在";
        }
        List<JournalField> journalFieldList2 = new ArrayList<>();
        boolean b = journalFieldList2.addAll(journalFieldList);
        if(!b){
            return "集合数据添加失败";
        }
        int count=0;
        for (JournalField journalField : journalFieldList) {
            if(journalField.getName()==null){
                continue;
            }
            List<JournalField> journalFields = new ArrayList<>();
            for (JournalField journalField2 : journalFieldList) {
                if(journalField2.getName()==null){
                    continue;
                }
                if(journalField.getName().equals(journalField2.getName())){
                    journalFields.add(journalField2);
                }
            }
            if(journalFields.size()<=1){
                continue;
            }
            //下面是针对journalFields.size()=2的情况
            int browseNum=0;
            String issn2="";
            for (int i=0;i<journalFields.size();i++) {
                JournalField field=journalFields.get(i);
                if(field.getIssn2()!=null){
                    field.setScore(field.getScore()+100);
                    if(!field.getIssn2().equals(issn2)){
                        field.setScore(field.getScore()+80);
                        issn2=field.getIssn2();
                    }else {
                        journalFields.get(i-1).setScore(journalFields.get(i-1).getScore()-80);
                    }

                }
                if(field.getIssn2()==null&&field.getIssn()!=null){
                    field.setScore(field.getScore()+40);
                }
                if(field.getCn()!=null){
                    field.setScore(field.getScore()+2);
                }
                if(field.getPrice()!=null){
                    field.setScore(field.getScore()+20);
                }
                if(field.getPublishingCycel()!=null){
                    field.setScore(field.getScore()+2);
                }
                if(field.getOrganizer()!=null){
                    field.setScore(field.getScore()+2);
                }
                if(field.getCreateTime()!=null){
                    field.setScore(field.getScore()+2);
                }
                if(field.getStatus()==null||(field.getStatus()!=null&&!field.getStatus().equals("目前状态：停刊"))){
                    field.setScore(field.getScore()+2);
                }
                if(field.getBrowseNum()!=null){
                    if(browseNum==0){
                        field.setScore(field.getScore()+1);
                        browseNum=Integer.parseInt(field.getBrowseNum());
                    }
                    else if(Integer.parseInt(field.getBrowseNum())>browseNum){
                        field.setScore(field.getScore()+1);
                        journalFields.get(i-1).setScore(journalFields.get(i-1).getScore()-1);
                    }
                }
            }
            JournalField journalField1=journalFields.get(0);
            for (int i=1;i<journalFields.size();i++) {
                if(journalFields.get(i).getScore()<journalField1.getScore()){
                    journalField1=journalFields.get(i);
                }
            }
            if(journalField1.getScore()<180&&journalFieldList2.contains(journalField1)){
                journalFieldList2.remove(journalField1);
                count++;
            }
            //把分数设置为0，避免影响下一轮
            for (int i=0;i<journalFields.size();i++) {
                journalFields.get(i).setScore(0);
            }
        }
        ExcelUtils.writeExcel(originalPath,journalFieldList2, JournalField.class);
        return "成功（去重了"+count+"个）";
    }

    @Override
    public String handleName(String poiPath) {
        List<Poi> poiList = ExcelUtils.readExcel(poiPath, Poi.class);
        if(poiList==null){
            return "文件不存在";
        }
        for (Poi poi : poiList) {
            if(poi.getName()==null){
                continue;
            }
            String name = poi.getName().replaceAll("\\([^)]*\\)", "");
            if(poi.getPname()!=null&&name.startsWith(poi.getPname())){
                name = name.replaceFirst("^"+poi.getPname(),"");
                if(name.length()<=3){
                    name = poi.getPname()+name;
                }
            }
            if(poi.getCityname()!=null&&name.startsWith(poi.getCityname())){
                name = name.replaceFirst("^"+poi.getCityname(),"");
                if(name.length()<=3){
                    name = poi.getCityname()+name;
                }
            }
            if(poi.getAdname()!=null&&name.startsWith(poi.getAdname())){
                name = name.replaceFirst("^"+poi.getAdname(),"");
                if(name.length()<=3){
                    name = poi.getAdname()+name;
                }
            }
            if(poi.getTownship()!=null&&name.startsWith(poi.getTownship())){
                name = name.replaceFirst("^"+poi.getTownship(),"");
                if(name.length()<=3){
                    name = poi.getTownship()+name;
                }
            }
            poi.setName(name);
        }
        ExcelUtils.writeExcel(poiPath, poiList, Poi.class);
        return "成功";
    }

    private String extractChinese(String s){
        String regex = "[\u4e00-\u9fa5a-zA-Z0-9]+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(s);
        StringBuilder sb = new StringBuilder();
        while (matcher.find()) {
            sb.append(matcher.group());
        }
        return sb.toString().toLowerCase();
    }

}
